<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix header">黑马面经运营后台</div>
      <el-form
        :model="loginFrom"
        :rules="rules"
        ref="loginFrom"
        class="login-from"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginFrom.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginFrom.password"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" class="loginbtn" @click="submitForm"
            >登录</el-button
          >
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
import { mapMutations } from 'vuex'
export default {
  name: 'LoginView',
  data () {
    return {
      loginFrom: {
        username: 'admin6',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '请输入账户名称', trigger: 'blur' },
          { min: 5, max: 11, message: '长度必须是5~11位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 11, message: '请输入5~11位的密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations('user', ['setToken']),
    submitForm () {
      this.$refs.loginFrom.validate(async valid => {
        if (!valid) return
        try {
          const res = await loginAPI(this.loginFrom)
          // console.log(res)
          this.setToken(res.data.token)
          this.$message.success('登录成功')
          this.$router.push('/')
        } catch {
          return this.$message.error('登录失败！')
        }
      })
    },
    resetForm () {
      this.$refs.loginFrom.resetFields()
    }
  },
  created () {}
}
</script>

<style lang="less" scoped>
.login-page {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(@/assets/login-bg.svg) no-repeat 50% / cover;
  .box-card {
    width: 420px;
    ::v-deep .el-card__header {
      padding: 0 !important;
      .header {
        width: 420px;
        font-size: 18px;
        color: #fff;
        background: #727cf5;
        text-align: center;
        line-height: 80px;
      }
    }
  }
  .login-from {
    padding: 0 20px;
    .btn {
      display: flex;
      justify-content: center;
      button {
        height: 40px;
        &.loginbtn {
          background: #727cf5;
          border-color: #727cf5;
        }
      }
    }
  }
}
</style>
